/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
const app = getApp()

Page({
  data: {
    config: app.globalData.config,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    goodsCategory: [
      {
        id: "1353731419288924162",
        parentId: "0",
        sort: 2,
        children: [
          {
            id: "1355427201956208642",
            parentId: "1353731419288924162",
            sort: 1,
            children: null,
            enable: "1",
            name: "iPhone",
            description: "",
            picUrl: "http://joolun-open.oss-cn-zhangjiakou.aliyuncs.com/10952adc-cad0-4c53-8762-9906d1dde220.jpg",
            createTime: "2021-01-30T16:06:42",
            updateTime: "2021-01-30T16:06:42",
            delFlag: "0"
          },
          {
            id: "1356512975786287105",
            parentId: "1353731419288924162",
            sort: 2,
            children: null,
            enable: "1",
            name: "Huawei",
            description: "",
            picUrl: "http://joolun-open.oss-cn-zhangjiakou.aliyuncs.com/goods-5.png",
            createTime: "2021-02-02T16:01:11",
            updateTime: "2021-02-02T16:01:11",
            delFlag: "0"
          },
          {
            id: "1356512975786287105",
            parentId: "1353731419288924162",
            sort: 3,
            children: null,
            enable: "1",
            name: "vivo",
            description: "",
            picUrl: "http://joolun-open.oss-cn-zhangjiakou.aliyuncs.com/goods-5.png",
            createTime: "2021-02-02T16:01:11",
            updateTime: "2021-02-02T16:01:11",
            delFlag: "0"
          }
        ],
        enable: "1",
        name: "手机",
        description: "",
        picUrl: "https://joolun-open.oss-cn-zhangjiakou.aliyuncs.com/%E5%88%86%E7%B1%BB-%E6%89%8B%E6%9C%BA.png",
        createTime: "2021-01-25T23:48:16",
        updateTime: "2021-02-20T10:54:40",
        delFlag: "0"
      },
      {
        id: "1356973226344960001",
        parentId: "0",
        sort: 3,
        children: null,
        enable: "1",
        name: "电脑",
        description: "",
        picUrl: "http://joolun-open.oss-cn-zhangjiakou.aliyuncs.com/%E5%88%86%E7%B1%BB-%E7%94%B5%E8%84%91.png",
        createTime: "2021-02-03T22:30:03",
        updateTime: "2021-02-20T10:54:40",
        delFlag: "0"
      },
      {
        id: "1352218128409477121",
        parentId: "0",
        sort: 4,
        children: null,
        enable: "1",
        name: "电子书",
        description: "",
        picUrl: "https://joolun-open.oss-cn-zhangjiakou.aliyuncs.com/%E5%88%86%E7%B1%BB-%E7%AE%B1%E5%8C%85%E9%85%8D%E9%A5%B0.png",
        createTime: "2021-01-21T19:34:59",
        updateTime: "2021-09-29T23:31:07",
        delFlag: "0"
      }
    ],
    load: true
  },
  onLoad() { 
    app.initPage()
      .then(res => {
        this.goodsCategoryGet()
      })
  },
  onShow() {
    //更新tabbar购物车数量
    wx.setTabBarBadge({
      index: 2,
      text: app.globalData.shoppingCartCount + ''
    })
  },
  goodsCategoryGet() {
    app.api.goodsCategoryGet()
      .then(res => {
        let goodsCategory = res.data
        this.setData({
          goodsCategory: goodsCategory
        })
      })
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    let that = this
    let list = this.data.goodsCategory
    let tabHeight = 0
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + i)
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight
          tabHeight = tabHeight + data.height
          list[i].bottom = tabHeight
        }).exec()
      }
      that.setData({
        load: false,
        goodsCategory: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (i - 1) * 50,
          TabCur: i
        })
        return false
      }
    }
  }
})
